from django.shortcuts import render,redirect,HttpResponse
from app01.models import Department,UserInfo,PhoneNumber
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
from django.utils.safestring import mark_safe
from app01.utils.pagination import Pagination
from app01.utils.bootstrapmodel import BootStrapModelForm
def depart_list(request):
    #去数据库中获取所有的部门列表
    depart_info=Department.objects.all()
    page_obj = Pagination(request, depart_info)
    context = {'depart_info': page_obj.page_queryset,
               "page_string": page_obj.html()}

    return render(request,'Department_list.html',context)

def depart_add(request):
    '''添加部门'''
    if request.method == 'GET':
        return render(request, "Department_add.html")

    departname=request.POST.get('title')
    Department.objects.create(title=departname)
    return redirect('/depart/list/')
def depart_del(request):
    id=request.GET.get('did')
    Department.objects.filter(id=id).delete()
    return redirect('/depart/list/')

#http://localhost:8000/depart/nid/edit/
#http://localhost:8000/depart/10/edit/ nid=10
def depart_edit(request,nid):
    '''修改部门'''
    #根据nid,获取它的数据
    if request.method=='GET':
        row_obj=Department.objects.filter(id=nid).first()
        return render(request,'department_edit.html',{'obj':row_obj})
    #获取用户提供的标题
    title=request.POST.get('title')
    # 根据ID找到数据库中的数据进行更新
    Department.objects.filter(id=nid).update(title=title)
    return redirect('/depart/list/')